#include <stdio.h>
#include <vector>
#include <map>
using std::vector;
using std::map;

class Element {
    map<int, int> m;
    int size;
public:
    Element(): size(0) {}
    void add(int n) {
        size++;
        m[n]++;
    }
    int majority() const {
        for (auto &kv : m) {
            if (kv.second > size/2) return kv.first;
        }
        return 0;
    }
};

int majorityElement(vector<int>& nums) {
	if (nums.size() == 1) return nums[0];
	Element element;

	for (auto n : nums) {
		element.add(n);
	}

	return element.majority();
}

int main() {
    vector<int> v {8,8,7,7,7};
    printf("out: %d\n", majorityElement(v));
}
